<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class HomeController extends Controller {

	protected $user;

	public function __construct(User $user) {
		$this->user = $user;
	}

	public function index(Request $request) {
		$user_id = $request->input('user_id');
		$from_id = $request->input('from');
		if (!$user_id || !is_numeric($user_id) || !$from_id || $from_id != 'afs8ce9ocnifu5ughpokn010t5' . date('Ymd')) {
			abort(401);
		}
		$user = $this->user->where('six_id', $user_id)->first();
		if (!$user) {
			abort(401);
		}
		if (!$user->is_admin) {
			$user->load('business');
			if (!count($user->business)) {
				abort(401);
			}
		}
		session(['user' => $user]);
		if ($user->is_admin) {
			return redirect()->route('business');
		}
		return redirect()->route('order');
	}
}
